// =============================================================================
//  Spangles Analyzer
//
//  Copyright(c) 2008
//  See LICENSE.txt for licensing information.
// =============================================================================

package com.google.spangles.interfaces.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;

import com.google.spangles.interfaces.Bounded;

/**
 * (Description) (Any invariants [conditions that are always true] ) (Usage
 * instructions, and/or examples) (TODOs, ASSUMPTIONs, etc.) (Known Bugs)
 * (Concurrency strategy - not thread safe or how it's thread safe)
 * 
 * @author Chris
 */
public class CompositeBounded implements Bounded
{
   Set<Bounded> boundedElements = new HashSet<Bounded>();

   public void addBounded(Bounded b)
   {
      boundedElements.add(b);
   }

   /*
    * (non-Javadoc)
    * 
    * @see com.google.spangles.interfaces.Bounded#getMaximumX()
    */
   public int getMaximumX()
   {
      Integer mx = null;
      for (Iterator<Bounded> bi = boundedElements.iterator(); bi.hasNext();)
      {
         Bounded b = bi.next();
         try
         {
            int m = b.getMaximumX();
            if ((mx == null) || (m > mx.intValue()))
            {
               mx = Integer.valueOf(m);
            }
         }
         catch (final NoSuchElementException e)
         {
            // ignore, it's something empty
         }
      }
      if (mx == null)
      {
         throw new NoSuchElementException();
      }
      return mx.intValue();
   }

   /*
    * (non-Javadoc)
    * 
    * @see com.google.spangles.interfaces.Bounded#getMaximumY()
    */
   public int getMaximumY()
   {
      Integer mx = null;
      for (Iterator<Bounded> bi = boundedElements.iterator(); bi.hasNext();)
      {
         Bounded b = bi.next();
         try
         {
            int m = b.getMaximumY();
            if ((mx == null) || (m > mx.intValue()))
            {
               mx = Integer.valueOf(m);
            }
         }
         catch (final NoSuchElementException e)
         {
            // ignore, it's something empty
         }
      }
      if (mx == null)
      {
         throw new NoSuchElementException();
      }
      return mx.intValue();
   }

   /*
    * (non-Javadoc)
    * 
    * @see com.google.spangles.interfaces.Bounded#getMinimumX()
    */
   public int getMinimumX()
   {
      Integer mx = null;
      for (Iterator<Bounded> bi = boundedElements.iterator(); bi.hasNext();)
      {
         Bounded b = bi.next();
         try
         {
            int m = b.getMinimumX();
            if ((mx == null) || (m < mx.intValue()))
            {
               mx = Integer.valueOf(m);
            }
         }
         catch (final NoSuchElementException e)
         {
            // ignore, it's something empty
         }
      }
      if (mx == null)
      {
         throw new NoSuchElementException();
      }
      return mx.intValue();
   }

   /*
    * (non-Javadoc)
    * 
    * @see com.google.spangles.interfaces.Bounded#getMinimumY()
    */
   public int getMinimumY()
   {
      Integer mx = null;
      for (Iterator<Bounded> bi = boundedElements.iterator(); bi.hasNext();)
      {
         Bounded b = bi.next();
         try
         {
            int m = b.getMinimumY();
            if ((mx == null) || (m < mx.intValue()))
            {
               mx = Integer.valueOf(m);
            }
         }
         catch (final NoSuchElementException e)
         {
            // ignore, it's something empty
         }
      }
      if (mx == null)
      {
         throw new NoSuchElementException();
      }
      return mx.intValue();
   }

}
